Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Stacked bar | sort ordering

    Hello all,

    How do I fix the sort order to show the PR above CR? I looked at other posts and the help and could not find the right option. I could sort it right in the khcomplex group 1 only here.

    graph bar, over(rttx1resp, sort(1)) nofill asyvars stack by(khcomplex) blabel(name,position(inside))


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str8 rttx1resp byte khcomplex
    ""         .
    ""         1
    ""         .
    "NR/SD/PD" 1
    "CR"       0
    ""         0
    ""         .
    "PR"       0
    "PR"       0
    "PR"       0
    "PR"       .
    ""         .
    "PR"       .
    "PR"       0
    ""         1
    "PR"       0
    ""         .
    ""         .
    ""         .
    "CR"       0
    "PR"       0
    ""         0
    ""         0
    "CR"       0
    "CR"       0
    ""         0
    "CR"       .
    ""         1
    "NR/SD/PD" 1
    "NR/SD/PD" 0
    "CR"       0
    "NR/SD/PD" 0
    "CR"       0
    "CR"       1
    "NR/SD/PD" 1
    ""         0
    ""         .
    "PR"       0
    "PR"       1
    "NR/SD/PD" .
    "PR"       0
    ""         1
    "NR/SD/PD" 1
    "NR/SD/PD" 0
    "NR/SD/PD" 0
    "PR"       1
    ""         .
    "NR/SD/PD" .
    ""         .
    "NR/SD/PD" 1
    "CR"       0
    ""         1
    "CR"       .
    "PR"       0
    ""         0
    ""         0
    "NR/SD/PD" 1
    "NR/SD/PD" .
    "NR/SD/PD" 0
    ""         0
    "PR"       .
    "CR"       0
    ""         0
    ""         .
    "NR/SD/PD" .
    "PR"       0
    "NR/SD/PD" 0
    ""         0
    "PR"       .
    ""         .
    "CR"       1
    "NR/SD/PD" 1
    "NR/SD/PD" 0
    "CR"       0
    ""         .
    ""         .
    ""         .
    ""         .
    "CR"       .
    "NR/SD/PD" 0
    "CR"       .
    "CR"       .
    "CR"       0
    "NR/SD/PD" 1
    ""         0
    "NR/SD/PD" 1
    ""         .
    "CR"       .
    ""         1
    ""         .
    "CR"       0
    ""         .
    ""         0
    "CR"       0
    ""         .
    "NR/SD/PD" .
    ""         .
    ""         1
    ""         .
    ""         1
    end

  • #2
    You need to define value labels indicating your desired order and then
    Code:
    encode
    accordingly. This may get you started:

    Code:
    . label def rttx1resp 1 "PR"
    
    . encode rttx1resp, gen(toshow) label(rttx1resp)
    
    . graph bar, over(toshow, sort(1)) nofill asyvars stack by(khcomplex) blabel(name,position(inside))
    But with the default scheme , and many others, the text descriptors are hard to read against the bar colours. My prejudice is that you would be better off with another design, such as https://www.statalist.org/forums/for...updated-on-ssc

    Comment


    • #3
      Originally posted by Nick Cox View Post
      You need to define value labels indicating your desired order and then
      Code:
      encode
      accordingly. This may get you started:

      Code:
      . label def rttx1resp 1 "PR"
      
      . encode rttx1resp, gen(toshow) label(rttx1resp)
      
      . graph bar, over(toshow, sort(1)) nofill asyvars stack by(khcomplex) blabel(name,position(inside))
      But with the default scheme , and many others, the text descriptors are hard to read against the bar colours. My prejudice is that you would be better off with another design, such as https://www.statalist.org/forums/for...updated-on-ssc
      I had forgotten that we could predefine value labels before encoding and applying them. Thanks much, Nick Cox
      I do agree that the the default color scheme does not cut it here. I will experiment with the alternative including hbar and catplot.

      Comment


      • #4
        graph hbar or catplot (SSC)will use the same colour sets as anything else unless you also change the scheme.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          graph hbar or catplot (SSC)will use the same colour sets as anything else unless you also change the scheme.
          I managed to get my own colors using -catplot- reflecting the good(CR), the bad(PR) and ugly(MR/SD/PD) as shades of green; not exactly color harmonious though.

          catplot rt1resp age75, percent(age75) asyvars bar(1, bcolor(green*.9)) ///
          bar(2, bcolor(green*.5)) ///
          bar(3, bcolor(green*.15)) ///
          stack subtitle(Age at transformation) ///
          blabel(name,position(center)) ///
          legend(off) ///
          name(resp_age75, replace)


          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input byte(rt1resp age75)
          . 0
          . 0
          . 1
          . 0
          . 1
          2 1
          5 1
          2 0
          5 1
          1 0
          . 0
          1 0
          5 0
          . 0
          . 0
          5 0
          . 0
          2 0
          1 1
          . 1
          2 1
          . 1
          5 0
          2 1
          . 0
          . 1
          2 0
          2 0
          1 0
          . 0
          2 0
          . 0
          . 1
          . 0
          5 0
          . 0
          . 0
          . 0
          . 1
          5 0
          . 0
          . 0
          . 1
          . 1
          2 0
          . 0
          5 0
          2 0
          . 1
          . 1
          . 1
          1 0
          5 0
          . 0
          . 0
          2 0
          5 0
          . 0
          . 0
          . 0
          . 0
          . 0
          . 1
          5 0
          . 0
          . 0
          2 0
          . 0
          2 0
          2 0
          . 0
          . 0
          . 0
          . 0
          . 0
          . 0
          2 0
          . 0
          1 1
          1 0
          1 0
          1 0
          1 0
          1 0
          5 1
          . 0
          5 0
          . 0
          5 0
          . 0
          . 0
          . 0
          . 0
          5 1
          1 0
          1 0
          2 0
          2 1
          . 0
          5 0
          end
          label values rt1resp resplab
          label def resplab 1 "CR", modify
          label def resplab 2 "PR", modify
          label def resplab 5 "NR/SD/PD", modify
          label values age75 age75lab
          label def age75lab 0 "<= 75 yrs", modify
          label def age75lab 1 ">75 yrs", modify

          Comment

          Working...
          X